package com.yonyou.crm.cum.customer.ctrl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.cum.customer.entity.CustomerRelusersVO;
import com.yonyou.crm.cum.customer.rmitf.ICustomerRelusersRmService;

@RestController
@RequestMapping(value = "/cum/customer/relusers")
public class CustomerRelusersCtrl {
	@Autowired
	private ICustomerRelusersRmService service;
	
	@RequestMapping(method = RequestMethod.GET)
	public Object list(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		Integer page = paramObj.getInt("page");
		Integer pageSize = paramObj.getInt("pageSize");
		@SuppressWarnings("unchecked")
		Map<String, Object> searchMap = (Map<String, Object>)paramObj.get("searchMap");
		if (page == null) {
        	throw new CrmMessageException("page不能为空");
		}
		if (pageSize == null) {
			throw new CrmMessageException("pageSize不能为空");
		}
		Page<CustomerRelusersVO> relusersPage = service.getList(new Page<CustomerRelusersVO>(page, pageSize), searchMap);
		return ResponseUtil.getObject(relusersPage);
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request,
			HttpServletResponse response) throws CrmMessageException {

		CustomerRelusersVO vo = RequestUtil.getVO(request,
				CustomerRelusersVO.class);
		if (vo == null) {
			throw new CrmMessageException("客户实体转换失败");
		}
		vo = service.insert(vo);
		if (vo != null && vo.getId() == null) {
			throw new CrmMessageException("获取自增主键失败，未保存成功");
		}
		return ResponseUtil.getObject(vo);
	}
	
	@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
	public Object delete(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		if (id == null) {
        	throw new CrmMessageException("id不能为空");
		}		
		return ResponseUtil.getObject(service.delete(id));
	}
	
	
}